package com.toobigtofail.shapes
{
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2Fixture;
	
	import fabulaexmachina.box2fp.Box2DEntity;
	import fabulaexmachina.box2fp.Box2DShapeBuilder;
	
	public class Box extends Box2DEntity implements IShape
	{
		private var _savedOptions:Object;
		
		public function Box(x:Number=0, y:Number=0, w:uint=1, h:uint=1, b2Type:int=0, group:int=0, category:int=0, collmask:int=0, friction:Number=0.3, density:Number=1, restitution:Number=0.3)
		{
			_savedOptions = {group: group, 
				             category: category, 
							 collmask: collmask, 
							 friction: friction, 
							 density: density, 
							 restitution: restitution,
						     b2Type: b2Type};
			
			super(x, y, w, h, b2Type, group, category, collmask, friction, density, restitution);
		}
		
		override public function buildShapes(friction:Number, 
									density:Number, restitution:Number,
									group:int, category:int, collmask:int):void {
			buildShape(body);
		}
		
		public function buildShape(body:b2Body):void {
			Box2DShapeBuilder.buildRectangle(body, Width/2, Height/2, _savedOptions.friction, _savedOptions.density, _savedOptions.restitution, _savedOptions.group, _savedOptions.category, _savedOptions.collmask);
			body.SetType(_savedOptions.b2Type);
		}
		
		public function get b2Type():int { return _savedOptions.b2Type; }
		
		public function serialize():XML {
			var shape:XML = <shape type="Box"/>;
			shape.@x = x;
			shape.@y = y;
			shape.@b2Type = b2Type;
			shape.@width = width;
			shape.@height = height;
			
			return shape;
		}
	}
}